var resourceUrl = getApp().request("imgUrl");
var requestUrl = getApp().request("url");
var app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headImgUrl: "",
    nickName: "",
    score:12314,
    myRank:1,
    animationData:"",
    bgModal:false,
    bgPromote: false,
    overPercent:"",
    resourceUrl: resourceUrl,
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //wx.hideShareMenu()
    this.loadRank();
    //获取本地的昵称和头像
    this.setData({
      headImgUrl: app.avatarUrl,
      nickName: app.nickName
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var animation=wx.createAnimation({
      transformOrigin:"85% 52%",
      duration:2000,
      timingFunction:"ease"
    });
    animation.rotate(360).step()
    this.setData({
      animationData: animation.export(),
      loading:false
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
      withShareTicket: true
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    //是否选车
    var fightCarTypeId = app.fightCarTypeId;
    //随机一句宣言
    var declaration = getApp().Refit();
    var myRefitTxt = Math.floor(Math.random() * declaration.length);
    if (fightCarTypeId == 0) {
      return {
        title: getApp().shareTitle1(),
        path: "/pages/index/index"
      }
    } else {
      return {
        title: getApp().shareTitle2(),
        path: "/pages/openShare/openShare?shareId=" + app.session_key.userId + "&carTypeId=" + fightCarTypeId + "&shareRefitNum=" + myRefitTxt + "&carName=SUV概念车",
        success: (re) => {
          if (Object.keys(re).length == 1) {
            return
          }
          var ticket = re.shareTickets[0];
          getApp().shareFn(ticket);
        }
      }
    }
  },
  btnKnow(){
    this.setData({
      bgModal: !this.data.bgModal
    })
  },
  btnPromoteBack(){
    this.setData({
      bgPromote: !this.data.bgPromote
    })
  },
  btnBack(){
    wx.navigateBack({
      delta:1
    })
  },
  loadRank(){
    var that=this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: requestUrl+'rank',
      method: "POST",
      data: { userId: app.session_key.userId, salt: app.session_key.salt },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success:function(res){
        console.log(res)
        if (res.statusCode != 200) {
          getApp().showModal("服务器异常");
          return
        }
        if (res.data.code == 200) {
          var myRank = "99+";    //我的名次
          var ownUserId = app.session_key.userId;
          var Rdata = res.data.data.r;
          for (var i = 0; i < Rdata.length;i++){
            if (ownUserId == Rdata[i][0]){
              myRank=i+1;
              break;
            }
          };
          var overPercent=null;
          if (myRank<=100){
            overPercent="99%";
          } else{
            myRank="99+";
            overPercent = "97%";
          }
          that.setData({
            myRank: myRank,
            overPercent: overPercent,
            rankData:res.data.data.r
          });
          wx.hideLoading();
        } else if (res.data.code == 800) {
          getApp().showModal(res.data.data.msg);
          wx.hideLoading();
        } else{
          console.log(res.data.code)
          getApp().showModal("提交信息有误!")
        }
      },
      fail: (fai) => {
        console.log("查询排名fail")
        console.log(fai)
        wx.hideLoading();
        getApp().showModal("网络超时，请检查您的网络");
      }
    })
  }
})